package com.gz.EuchreU;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.layer.Layer;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;


public abstract class LoadingGameActivity2 extends BaseGameActivity {
 
	// ===========================================================
	// Fields
	// ===========================================================
 
	protected Engine engine;
 
	private Texture loadScreenBGTexture;
	private TextureRegion loadScreenBGRegion;
 
	private Texture carTexture;
	private TextureRegion carRegion;
 
	// ===========================================================
	// Constants
	// ===========================================================
 
	protected static final float CAMERA_WIDTH = 320, CAMERA_HEIGHT = 480;
	protected static final int AD_SIZE_Y = 50;
	protected static final int PHYSICS_TIME_STEP = 60;
 
 
	// ===========================================================
	// Inherited Methods
	// ===========================================================
 
	public Engine onLoadEngine() {
		Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return engine = new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera));
	}
 
	public void onLoadResources() {
		TextureRegionFactory.setAssetBasePath("gfx/load/");
 
		loadScreenBGTexture = new Texture(512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		loadScreenBGRegion = TextureRegionFactory.createFromAsset(loadScreenBGTexture, this, "logo.png", 0, 0);
 
		carTexture = new Texture(128,128);
		carRegion = TextureRegionFactory.createFromAsset(carTexture, this, "car.png", 0, 0);
 
 
		engine.getTextureManager().loadTextures(loadScreenBGTexture, carTexture);
	}
 
	/**
	  * This is where you create your loading screen. I've built it with a
  	  * physics world to demonstrate that you can use it.
	  */
	public Scene onLoadScene() {
		Scene scene = new Scene(1);
		//scene.setBackground(new ColorBackground(10, 200, 10));
 
		// display loading logo
		Sprite s = new Sprite(100, 100, loadScreenBGRegion);
		s.setPosition((CAMERA_WIDTH / 2) - (s.getWidthScaled() / 2), (CAMERA_HEIGHT / 3) - (s.getHeightScaled() / 2));
		scene.getLastChild().attachChild(s);
		// car fixturedef
		FixtureDef carDef = PhysicsFactory.createFixtureDef(1, 3, 0);
 
		// create initial car
		Sprite queCar = new Sprite((CAMERA_WIDTH / 5) * 1, (CAMERA_HEIGHT / 3) * 2, carRegion);
		scene.getLastChild().attachChild(queCar);
 
		// create second car
		Sprite ballCar = new Sprite((CAMERA_WIDTH / 5) * 3, (CAMERA_HEIGHT / 3) * 2, carRegion);
		scene.getLastChild().attachChild(ballCar);
  
		/*
		 * Here's where the assets are loaded in the background behind the loading scene.
		 */
		IAsyncCallback callback = new IAsyncCallback() {
 
			public void workToDo() {
				assetsToLoad();
			}
 
			public void onComplete() {
				unloadLoadingScene();
				engine.setScene(onAssetsLoaded());
			}
		};
 
		new AsyncTaskLoader().execute(callback);
 
		return scene;
	}
 
	// ===========================================================
	// Methods
	// ===========================================================
 
	private void unloadLoadingScene(){
 
	}
 
	/**
	 * This will be called after all of the asyc assets are loaded.
	 * The loader will be in charge of changing the scenes.
	 */
	protected abstract Scene onAssetsLoaded();
 
 
	/**
	 * This is called when assets need to be loaded in the background.
	 */
	protected abstract void assetsToLoad();
}
